package com.example.covid19.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.OrderForm;
import com.example.covid19.entity.Recharge;
import com.example.covid19.entity.User;
import com.example.covid19.mapper.OrderFormMapper;
import com.example.covid19.mapper.RechargeMapper;
import com.example.covid19.service.OrderFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.covid19.service.UserService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@Service
public class OrderFormServiceImpl extends ServiceImpl<OrderFormMapper, OrderForm> implements OrderFormService {
    private final UserService userService;

    private final RechargeMapper rechargeMapper;

    public OrderFormServiceImpl(UserService userService, RechargeMapper rechargeMapper) {
        this.userService = userService;

        this.rechargeMapper = rechargeMapper;
    }

    @Override
    public Result Recharge(JSONObject object){
        int totalAmt = object.getInt("totalAmt");
        LocalDateTime dateTime = LocalDateTime.now();
        String uuid = UUID.randomUUID().toString();
        System.out.println(totalAmt);
        User user = new User();
        Recharge recharge = new Recharge();
        recharge.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
        recharge.setMontant(totalAmt);
        recharge.setTimer(dateTime);
        recharge.setUuid(uuid);
        user.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
        user.setBalance(userService.getOne(new QueryWrapper<User>().eq(
                "userid",StpUtil.getLoginId()
        )).getBalance()+totalAmt);
        User user2 = userService.getOne(new QueryWrapper<User>().eq(
                "userid", StpUtil.getLoginId()
        ));
        recharge.setAlors(user2.getBalance());
//        user.setBalance(totalAmt);
        boolean flag = userService.updateById(user);
        if (flag){
            rechargeMapper.insert(recharge);
            return Result.success(200,"成功",null);
        }
        else
            return Result.fail(400,"失败",null);

    }
}
